package com.syh.jn.scm.service.impl.operation.supplier;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.operation.entity.supplier.OperaSupplier;
import com.syh.jn.scm.domain.operation.entity.supplier.OperaSupplierAccount;
import com.syh.jn.scm.domain.operation.event.supplier.OperaSupplierEvent;
import com.syh.jn.scm.domain.operation.repository.supplier.OperaSupplierAccountReadRepository;
import com.syh.jn.scm.domain.operation.repository.supplier.OperaSupplierRepository;
import com.syh.jn.scm.domain.operation.repository.system.OperationAccountRepository;
import com.syh.jn.scm.dto.operation.supplier.*;
import com.syh.jn.scm.service.api.operation.supplier.OperaSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class OperaSupplierServiceImpl extends AbstractServiceImpl<OperaSupplierSto, OperaSupplierMto, OperaSupplierVo, OperaSupplier, Long> implements OperaSupplierService {
  @Autowired
  private OperaSupplierRepository operaSupplierRepositoryImpl;
  @Autowired
  private OperationAccountRepository operaAccountRepositoryImpl;
  @Autowired
  private OperaSupplierAccountReadRepository operaSupplierAccountRepositoryImpl;

  @Override
  protected AggregateRepository<OperaSupplier, Long> getAggregateRepository() {
    return operaSupplierRepositoryImpl;
  }

  /**
   * 运营端新增供应商
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(OperaSupplierSto sto) {
    OperaSupplier operaSupplier = new OperaSupplier(sto);
    Long id = operaSupplierRepositoryImpl.add(operaSupplier);
    operaSupplier.setId(id);
    this.publicEvent(OperaSupplierEvent.bulidOperaSupplierCreated(new Object[]{operaSupplier}));
    return id;
  }

  /**
   * 运营端修改供应商
   *
   * @param mto
   */
  @Override
  public void update(OperaSupplierMto mto) {
    OperaSupplier operaSupplier = operaSupplierRepositoryImpl.getById(mto.getId());
    operaSupplier.modify(mto);
    operaSupplierRepositoryImpl.update(operaSupplier);
  }

  /**
   * 根据id删除主体和地址项
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    OperaSupplier operaSupplier = operaSupplierRepositoryImpl.getById(id);
    operaSupplier.destroy();
    operaSupplierRepositoryImpl.remove(operaSupplier);
  }

  /**
   * 启用
   *
   * @param id
   */
  @Override
  public void enabled(Long id) {
    OperaSupplier operaSupplier = operaSupplierRepositoryImpl.getById(id);
    operaSupplier.enabled();
    operaSupplierRepositoryImpl.enabled(operaSupplier);
  }

  /**
   * 停用
   *
   * @param id
   */
  @Override
  public void disabled(Long id) {
    OperaSupplier operaSupplier = operaSupplierRepositoryImpl.getById(id);
    operaSupplier.disabled();
    operaSupplierRepositoryImpl.disabled(operaSupplier);
  }

  /**
   * 添加供应商账号
   *
   * @param sto
   * @return
   */
  @Override
  public Boolean addAccount(OperaSupplierAccountSto sto) {
    OperaSupplierAccount account = new OperaSupplierAccount(sto);
    OperaSupplier operaSupplier = operaSupplierRepositoryImpl.getById(sto.getBusinessEntityId());
    return operaSupplierAccountRepositoryImpl.addAccount(account);
  }

  /**
   * 修改供应商账号
   *
   * @param mto
   * @return
   */
  @Override
  public Boolean setAccount(OperaSupplierAccountMto mto) {
    OperaSupplierAccount operaSupplierAccount = operaSupplierAccountRepositoryImpl.getById(mto.getId());
    operaSupplierAccount.modify(mto);
    ExtBeanUtils.copyProperties(mto, operaSupplierAccount, false);
    operaSupplierAccountRepositoryImpl.update(operaSupplierAccount);
    return true;
  }

  @Override
  public List<OperaSupplierAddressVo> getAddressListById(Long id) {
    return operaSupplierRepositoryImpl.getAddressListById(id);
  }

  @Override
  public void openAccount(Long id) {
    OperaSupplier operaSupplier = operaSupplierRepositoryImpl.getById(id);
    operaSupplier.openAccount();
    operaSupplierRepositoryImpl.update(operaSupplier,false);
  }

  @Override
  public void reversalAccount(Long id) {
    OperaSupplier operaSupplier = operaSupplierRepositoryImpl.getById(id);
    operaSupplier.reversalAccount();
    operaSupplierRepositoryImpl.update(operaSupplier,false);
    this.publicEvent(OperaSupplierEvent.bulidSupplierReversalAccount(new Object[]{operaSupplier}));

  }
}
